<?php
include_once IR."functions/user/reg.func.php"; 
include_once IR."config/define/define.wordslength.php";
include_once IR."config/define/define.reg.php";
include_once IR.'functions/player/player.func.php';
include_once IR.'functions/club/club.func.php';
include_once IR.'functions/instance/ins.func.php';
include_once IR.'functions/buddy/buddy.func.php';

$json_rst = array('rstno'=>1,'error'=>'error','url'=>'');
$pp_uid = !empty($_REQUEST['pp_uid']) ? trim(mysql_real_escape_string($_REQUEST['pp_uid'])) : ('');
$authkey = !empty($_REQUEST['authkey']) ? trim(mysql_real_escape_string($_REQUEST['authkey'])) : ('');
$email = !empty($_REQUEST['email']) ? trim(mysql_real_escape_string($_REQUEST['email'])) : '';
$ts = !empty($_REQUEST['ts']) ? (int)$_REQUEST['ts'] : ('');
$typ = !empty($_REQUEST['typ']) ? (int)$_REQUEST['typ'] : 0; 
$obsession = !empty($_REQUEST['obsession'])? (int)$_REQUEST['obsession']:0;
$email = !empty($_REQUEST['email'])?trim(mysql_real_escape_string($_REQUEST['email'])):'';
$pwd = !empty($_REQUEST['pwd'])?trim(mysql_real_escape_string($_REQUEST['pwd'])):'';
$rpwd = !empty($_REQUEST['rpwd'])?trim(mysql_real_escape_string($_REQUEST['rpwd'])):'';
$mname = !empty($_REQUEST['mname'])?trim(mysql_real_escape_string($_REQUEST['mname'])):$pp_uid;
$cname = !empty($_REQUEST['cname'])?trim(mysql_real_escape_string($_REQUEST['cname'])):$pp_uid;
$agree = !empty($_REQUEST['agree'])?trim(mysql_real_escape_string($_REQUEST['agree'])):'';

$nid = $g_default_name_nid;
$uid = !empty($_REQUEST['uid'])?(int)$_REQUEST['uid']:0;
$role = !empty($_REQUEST['role'])?(int)$_REQUEST['role']:0;

$invite_zone_id = isset($_REQUEST['invite_zone_id'])?(int)$_REQUEST['invite_zone_id']:0;
$invite_uid = isset($_REQUEST['invite_uid'])?(int)$_REQUEST['invite_uid']:0;
if ($invite_zone_id!=0 && $invite_uid!=0){
    $intro = 1;
} else {
    $intro = 0;
}

$nameconflict = '';
$nameconflict_id = 1;
if($g_version_op==1){
    $aa_api_data = new QQAPI();
    //$aa_api_data->check_login();
    $pp_uid = $aa_api_data->getUID();    
}
//echo $pp_uid;
//exit;
if( check_pp_uid_exists($pp_uid)==true ) {	 
    $nameconflict_id = -7;
    //直接登录
    $url  = './';
    $json_rst['url'] = $url;
    return;
}
if ($config_open_reg==0){
    $json_rst['rstno'] = -1;
    $json_rst['error'] = _('本服务器已经关闭注册');
    return;
}
if ($g_version_op!=1){
    //联运注册验证
    if(abs($ts-time())>900){
        $json_rst['rstno'] = -1;
        $json_rst['error'] = _('注册超时');
        return;
    }
    if($authkey != md5( $pp_uid . md5($ts. $public_key.md5($sys_id.$public_key)))){ 
         
        $json_rst['rstno'] = -5;
        $json_rst['error'] = _('签名失败');
        return;
    }
    //qq不检查mname
    if (mb_strlen($mname,'UTF-8')>$words['mname']['max']){    
	$nameconflict_id = -6;   
    } elseif (mb_strlen($mname,'UTF-8')<$words['mname']['min']){	 
        $nameconflict_id = -2;
    } elseif (reg_special_word_return($mname))	{ 
        $nameconflict_id = -4;
    }elseif (isexist_mname($mname) ) {	 
        $nameconflict_id = -3;
    }
}
if (mb_strlen($cname,'UTF-8')>$words['cname']['max']){	 
    $nameconflict_id = -1;
} elseif (mb_strlen($cname,'UTF-8')<$words['cname']['min']){	 
    $nameconflict_id = -2; 
} elseif (isexist_cname($cname) ) {	 
    $nameconflict_id = -5; 
} else if (reg_special_word_return($cname))	{ 
    $nameconflict_id = -4;
}

if( $nameconflict_id!=1 ){
    $json_rst['rstno'] = $nameconflict_id;
    $json_rst['error'] = _('error');
    return;
}

	 
    $uid = create_user($uid,$pp_uid,$intro,$obsession);
    if($uid==-1){
        $json_rst['rstno'] = -8;
        $json_rst['error'] = _('用户已创建');
        return;
    }
    if ($g_version_op==1){		
        buddy_update_qq_buddy_list($uid);        
    }
	
    $cid = $uid;
    $credits = 100;
  
    $pid_list = array();
    $sql_squad_list = array();
    $club_att_value = 0;
    $club_def_value = 0;
    $player_squad_list = array();
    foreach($reg_config_player_list as $key=>$v){
        $main_pos = $v['main_position_typ'];
        $kit_number = $v['kit_number'];
        $pid_key = $cid%count($config_reg_player[$key]);
        $rp_id = $config_reg_player[$key][$pid_key];
        $this_player = new real_player($rp_id);
        $pid = $this_player->copy_user_player($cid,$kit_number,'',0,0,0,0,0,2);
     
        $this_player_att_defend = $this_player->get_player_att_defend_by_level(1);
        $position_id = $v['position_id'];
        if($position_id!=0){
            if($position_id<12){
                $is_suitable = 1;
                $club_att_value += $this_player_att_defend['att_value'];
                $club_def_value += $this_player_att_defend['def_value'];
            }else{
                $is_suitable = -1;
            }
            $player_squad_list[$pid] = array('pid'=>$pid,'cid'=>$cid,'position_id'=>$position_id,'is_suitable'=>$is_suitable);
            $sql_squad_list[] = "($pid,$cid,$position_id,$is_suitable)";            
        }
        $pid_list[] = $pid;
    }
    player_update_squad_list($cid,$player_squad_list); 
    _update_player_id_list_in_team($cid,$pid_list);
    $sql = "INSERT INTO c_club_info(`cid`, `club_name`,`manager_name`, 
            `club_money`, `credits`, `rating_lev`, `rating_total`,`formation_id`,`formation_level`,`tactic_slot_count`,`attr_points_can_use`,`att_value`,`def_value`
        )
        VALUES ($cid, '$cname','$mname',100000,$credits,1,0,21,1,1,500,$club_att_value,$club_def_value)";
    mysql_w_query($sql);
	
    if($g_version_flag==1){
        $obj_analyze = my::get_instance($pp_uid,$udp_log_config);		
        $obj_analyze->Log_msg('install',$credits,0,'add','','','currency');
    }
     
    //初始化阵容
    if(!empty($sql_squad_list)){
        $sql_squad_list_str = implode(',',$sql_squad_list);
        $sql = "INSERT INTO c_squad (pid,cid,position_id,is_suitable) VALUES $sql_squad_list_str";
        mysql_w_query($sql);
    }
    
    //赠送阵型战术
    $sql = "INSERT INTO c_formation (cid,formation_id,level,show_or_hide) VALUES ($cid,21,1,1)";
    mysql_w_query($sql);
    $tactic_base_rate = update_tactic_chance($cid,1,21);
    $sql = "INSERT INTO c_tactic (cid,tactic_id,in_use,chance) VALUES($cid,1,1,$tactic_base_rate)";
    mysql_w_query($sql);
 
    $sql = "INSERT INTO `p_player_youth_study_site`(`cid`,`study_lev`) VALUES ($cid,1)";
    mysql_w_query($sql);
	
    //生成雇员
    $sql_insert = array();
    for($i=1;$i<=5;$i++){
        $employe_lev = 1;		 
        $nid = 105;
        $name = player_gen_name($nid);
        $employee[$i] = array( 'typ'=>$i,
            'employe_name'=>$name['player_name'],
            'employe_lev'=>1);
    }
    $extend_info['cid'] = $cid;
    $extend_info['employee'] = $employee;
    //club_write_back_extend_data($cid,$extend_info);
    set_cache($cid,'club_info_extend',$extend_info,1);
    $sql = "INSERT INTO c_club_info_extend (cid,json_data) VALUES($cid,'".mysql_real_escape_string(json_encode($extend_info))."')";;
    mysql_x_query($sql);
    
     

 
$ts = time();
$authkey = md5( $pp_uid . md5($ts. $public_key.md5( $sys_id .$intro)));

$sid = gen_session_id($uid,$zeit,$public_key);
$online_zeit = $zeit;

$online_id = gen_online_id($uid,$sid,$online_zeit);
set_cookie($uid, $sid, $uid,$pp_uid,$online_id); 
init_user_session_and_cache($uid,$sid);	
if ($g_version_op==1){		
    if ($invite_zone_id!=0 && $invite_uid!=0){
        $send_award_invite = 1;
        //发邀请礼包
        $unique_id = $invite_zone_id.'_'.$invite_uid;
        $sql = "SELECT count(*) as count_i FROM gift_voucher WHERE verify_num='$unique_id'";     
        $rst = mysql_w_query($sql);
        if($row = mysql_fetch_assoc($rst)){
            if ($row['count_i']>=1000){
                $send_award_invite=0;
            }
        }
        if ($send_award_invite==1){
            //检查及插入邀请信息
            db_news();
            $sql = "SELECT * FROM invite_info WHERE pp_uid ='$pp_uid'";
            $rst = mysql_x_query($sql);
            if (mysql_num_rows($rst)>=1){
                $send_award_invite=0;
            }
            if ($send_award_invite==1){
                //写回各种信息
                $sql = "INSERT INTO invite_info
                            (zone_id,uid,pp_uid,by_zone_id,by_uid,zeit,total_awards_credits,total_awards_rcredits)
                            VALUES
                            ($zone_id,$uid,'$pp_uid',$invite_zone_id,$invite_uid,$zeit,0,0)";
                mysql_x_query($sql);
                
                $sql = "SELECT * FROM u_invite_awards WHERE zone_id =$invite_zone_id AND uid=$invite_uid";
                $rst = mysql_x_query($sql);
                if (mysql_num_rows($rst)>=1){
                    $sql = "UPDATE u_invite_awards
                    SET invite_counter=invite_counter+1
                    WHERE zone_id =$invite_zone_id AND uid=$invite_uid";
                    mysql_x_query($sql);
                } else {
                    $sql = "INSERT INTO u_invite_awards
                            (zone_id,uid,invite_counter,total_awards_credits,total_awards_real_credits,get_awards_credits,get_awards_real_credits)
                            VALUES
                            ($invite_zone_id,$invite_uid,1,0,0,0,0)";
                    mysql_x_query($sql);
                }
                db_connect();
                //发奖
                club_add_money($uid,8,1000,2);
            }
            db_connect();
        }
        
    }
}

//送新手礼包
item_user_get_shop_item($cid,16,3,1,3);
if($g_version_flag==1){
    $obj_analyze = my::get_instance($pp_uid,$udp_log_config);		
    $obj_analyze->log_msg('load','success','','','','','guide');	
    $obj_analyze->log_msg('guide',100,$uid,4,5,6,'guide');
} 
if( $zeit<$config['game_start'] && !in_array($uid,$config['maintain_super_uid']))
{
    //echo 1;
    return_not_open();
    exit;
}

if($role==1){
    //选择我是老手
    //出新手期
    $json_rst['new_newbie_stat'] = 1;
    $user_base = user_get_user_base($uid);
    $sql = "UPDATE u_user SET newbie_stat=1 WHERE uid=$uid";
    mysql_w_query($sql);
    $user_base['newbie_stat'] = 1;
    set_cache($uid,'user_base_info',$user_base,1);

    //user_update_user_base($uid);
    _update_user_task_get_task_list($uid);
    //插入第一条主线任务
    user_task_init_task($uid,1101);
    user_task_init_task($uid,1301);
    buddy_check_inviter($uid,$pp_uid);

    $club_info = club_get_club_info($uid);
    $club_info_changes = array('league_status','rating_lev','rating_total','club_money');
    $club_info['league_status'] = 1;
    $club_info['rating_lev'] = 6;
    $club_info['rating_total'] = 1000;
    $club_info['club_money'] = 90000;

    club_write_back_main_data($uid,$club_info,$club_info_changes);
    news_new_reg($uid,9903,0,10);

    //球探刷球员
    player_refresh_player_list($uid,1);
    //选联赛
    ins_init_new_league($uid,2);
    if($g_version_flag==1){
        $obj_analyze = my::get_instance($pp_uid,$udp_log_config);
        $obj_analyze->log_msg('guide',1110,$uid,4,5,6,'guide');
    }

}
//echo 222;
$url  = './';
//echo $url;
$json_rst['url'] = $url;
 
?>
